//
//  DiagnosisTouchViewController.m
//  MobileChecker
//
//  Created by Lymons on 15-1-7.
//  Copyright (c) 2015年 Lymons. All rights reserved.
//

#import <CoreMotion/CoreMotion.h>

#import "DiagnosisTouchViewController.h"
#import "SelfDiagnosisViewModel.h"
#import "TouchPadView.h"

@interface DiagnosisTouchViewController ()

@property (strong, nonatomic) IBOutlet UIButton *resultOkButton;
@property (strong, nonatomic) IBOutlet UIButton *resultNgButton;
@property (strong, nonatomic) IBOutlet UILabel *headerLabel;

@property (strong, nonatomic) TouchPadView *padView;

@end

@implementation DiagnosisTouchViewController

- (UIView *)viewForDoubleTap
{
    if (_padView == nil) {
        _padView = [[TouchPadView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
//        UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
//        label.text = @"曲線描画を終了する場合に\n画面をダブルタップしてください。";
//        label.numberOfLines = 0;
//        label.textAlignment = NSTextAlignmentCenter;
//        label.textColor = [UIColor darkTextColor];
//        label.font = [UIFont systemFontOfSize:14];
//        label.backgroundColor = [UIColor clearColor];
//        [label sizeToFit];
//        [label setCenter:_padView.center];
//        [_padView addSubview:label];
    }
    return _padView;
}

- (void)handleSingleTap:(id)x
{
    NSLog(@"Touch begining");
    [self showFullscreenModalViewWithView:self.padView animated:YES completion:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self handleDoubleTap:x];
        });
    }];
}

- (void)handleDoubleTap:(id)x
{
    NSLog(@"Touch end");
    [self dismissFullscreenModalViewWithView:self.padView animated:YES completion:NULL];
    [self.viewModel checkDidCompleted];
    
    self.resultOkButton.enabled = YES;
    self.resultNgButton.enabled = YES;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.resultOkButton.enabled = NO;
    self.resultNgButton.enabled = NO;
}

#pragma mark -

- (void)bindViewModel {
    [super bindViewModel];
    
    self.viewModel = [[DiagnosisTouchViewModel alloc] init];
    
    void (^resultBlock)(BOOL) = ^(BOOL result) {
        NSLog(@"Touch is OK : %@", result ? @"YES" : @"NO");
        [self performSegueWithIdentifier:@"TouchCompleted" sender:self];
    };
    self.resultOkButton.rac_command = [self.viewModel makeCommandWithBlock:resultBlock simple:YES];
    self.resultNgButton.rac_command = [self.viewModel makeCommandWithBlock:resultBlock simple:NO];
    RAC(self, headerLabel.text) = RACObserve(self, viewModel.headerString);
}

#pragma mark -

-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    [self.padView clear];
}

@end
